package FrontEnd.Helper;

import java.util.ArrayList;

import Entities.CHillEstimator;
import Entities.CRendite;

public class HillEstimator {

	private static HillEstimator instance;
	private ArrayList<CHillEstimator> hill;
	private ArrayList<CRendite> liste;

	public static synchronized HillEstimator getInstance(){
		if(instance == null){
			instance = new HillEstimator();
		}
		return instance;
	}
	
	public HillEstimator(){
		liste = new ArrayList<CRendite>();
	}

	public ArrayList<CHillEstimator> calcHillEstimator(ArrayList<CRendite> list){
		this.liste = list;
		this.hill = new ArrayList<CHillEstimator>();
		CHillEstimator hillEst = null;
		
		hillEst = new CHillEstimator(Double.NaN,1, list.get(0).getRendite());
		hill.add(hillEst);
		for(int i=1;i<list.size();i++){
			double d = this.calc(i);
			hillEst = new CHillEstimator(d,i+1, list.get(i).getRendite());
			hill.add(hillEst);
		}
		return hill;
	}

	public void printHillEstimator(){
		for (int i=0;i<hill.size();i++){
			System.out.println(hill.get(i).getN() + " " +  hill.get(i).getRendite() + " " + hill.get(i).getHillEsti());
		}
	}
	
	
	private double calc(int bis){
		double sum = 0;
		for (int i=0; i<=bis;i++){
			sum = sum + this.calcLogOfTwoValues(liste.get(i).getRendite(), liste.get(bis).getRendite());
		}
		sum = (Math.pow(sum/(bis+1),-1));
		return sum;
	}
	
	private double calcLogOfTwoValues(double val1, double val2){
		return Math.log(val1)-Math.log(val2);
	}
	
}
